package com.stylefeng.guns.modular.dataInterface;

import com.alibaba.druid.support.json.JSONUtils;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.modular.bank.service.IBankService;
import com.stylefeng.guns.modular.expenditure.service.IExpenditureService;
import com.stylefeng.guns.modular.revenue.model.Revenue;
import com.stylefeng.guns.modular.revenue.service.IRevenueService;
import com.stylefeng.guns.modular.system.model.Bank;
import com.stylefeng.guns.modular.system.model.Expenditure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 描述:导入数据
 * 创建人: xiang
 * 日期: 2018/8/13
 * 时间: 16:12
 */
@Controller
@RequestMapping("/platformData")
public class PlatformDataController {

    @Autowired
    private IRevenueService revenueService;

    @Autowired
    private IBankService bankService;

    @Autowired
    private IExpenditureService expenditureService;


    /**
     * 描述:导入平台数据
     * 创建人: xiang
     * 日期: 2018/8/13
     * 时间: 11:51
     */
    @RequestMapping(value = "importData")
    @ResponseBody
    public Object importData(@RequestParam(required = true) String jsondata ) throws Exception {

        Map<String,Object> map=  (Map<String,Object> )JSONUtils.parse(jsondata);
        String date=map.get("date").toString();
        String platform=map.get("platform").toString();
        Map<String,Object> revenuemap=null;
        if(map.get("userDailyReport")!=null){
            revenuemap= (Map<String,Object>) map.get("userDailyReport");
            insertRevenueAndExpenditure(date,platform,revenuemap);
        }
        List<Map<String,Object>> bankmap=null;
        if(map.get("banks")!=null){
            bankmap=(List<Map<String,Object>> )map.get("banks");
            insertBank(date,platform,bankmap);
        }
        return null;

    }




    /**
     * 描述:写入盈亏和银行日结
     * 创建人: xiang
     * 日期: 2018/8/13
     * 时间: 20:32
     */
    private void insertRevenueAndExpenditure(String date, String platform,Map<String,Object> revenuemap){
        Revenue revenue =new Revenue();
        Expenditure expenditure =new Expenditure();
        revenue.setRevtime(DateUtil.parseDate(date));
        revenue.setPlatform(platform);
        revenue.setCreattime(new Date());
        revenue.setAgProfit(-(Double.valueOf(revenuemap.get("agvideowinmoneytotal").toString())
                +Double.valueOf(revenuemap.get("agselfbonusmoney").toString())));
        revenue.setBbinProfit(-(Double.valueOf(revenuemap.get("bbinvideowinmoneytotal").toString())
                +Double.valueOf(revenuemap.get("bbselfbonusmoney").toString())));
        revenue.setPtProfit(-(Double.valueOf(revenuemap.get("ptvideowinmoneytotal").toString())
                +Double.valueOf(revenuemap.get("ptselfbonusmoney").toString())));
        revenue.setDsProfit(-(Double.valueOf(revenuemap.get("dsvideowinmoneytotal").toString())
                +Double.valueOf(revenuemap.get("dsselfbonusmoney").toString())));
        revenue.setMgProfit(-(Double.valueOf(revenuemap.get("mgvideowinmoneytotal").toString())
                +Double.valueOf(revenuemap.get("mgselfbonusmoney").toString())));
        revenue.setGpProfit(-(Double.valueOf(revenuemap.get("gpvideowinmoneytotal").toString())
                +Double.valueOf(revenuemap.get("gpselfbonusmoney").toString())));
        revenue.setSbProfit(-(Double.valueOf(revenuemap.get("sbvideowinmoneytotal").toString())
                +Double.valueOf(revenuemap.get("sbselfbonusmoney").toString())));
        revenue.setKgProfit(-(Double.valueOf(revenuemap.get("kjlotterywinmoneytotal").toString())
                +Double.valueOf(revenuemap.get("kjlotteryselfbonusmoney").toString())));
        revenue.setLotterProfit(-(Double.valueOf(revenuemap.get("lotterywinmoneytotal").toString())));
        revenue.setRebateProfit( -(Double.valueOf(revenuemap.get("lotteryselfbonusmoney").toString())
                +Double.valueOf(revenuemap.get("agentbonusmoneytotal").toString())));
        revenue.setHdzsProfit(-(Double.valueOf(revenuemap.get("hdzs").toString())));
        this.revenueService.insert(revenue);

        expenditure.setRevdate(DateUtil.parseDate(date));
        expenditure.setPlatform(platform);
        expenditure.setCreattime(new Date());
        expenditure.setRecharge(Double.valueOf(revenuemap.get("inmoneytotal").toString()));
        expenditure.setReflect(Double.valueOf(revenuemap.get("outmoneytotal").toString()));
        this.expenditureService.insert(expenditure);


    }

    /**
     * 描述:插入银行余额
     * 创建人: xiang
     * 日期: 2018/8/13
     * 时间: 20:17
     */
    private  void insertBank(String date, String platform,List<Map<String,Object>> bankmap){
        List<Bank> banks= new ArrayList<Bank>();
        for(int i=0;i<bankmap.size();i++){
            Map<String,Object> bank=bankmap.get(i);
            int banktype=Integer.valueOf(bank.get("state").toString());
            Bank bank1= new Bank();
            bank1.setStatus(1);
            bank1.setBanktype(banktype);
            bank1.setCreattime(new Date());
            bank1.setBalance(Double.valueOf(bank.get("balance").toString()));
            bank1.setBankName(bank.get("bankname").toString());
            bank1.setUsername(bank.get("accountname").toString());
            bank1.setPlatform(platform);
            bank1.setRevtime(DateUtil.parseDate(date));
            if(Bank.Enum_BankType.入款银行.value.intValue()==banktype){
                bank1.setRsemarks("入款卡");
            }if(Bank.Enum_BankType.出款银行.value.intValue()==banktype){
                bank1.setRsemarks("出款卡");
            }
            banks.add(bank1);
        }

        this.bankService.insertBatch(banks);


    }



}
